﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Activite
    /// </summary>
    public abstract class ActiviteOPManager
    {
        /// <summary>
        /// Méthode de récupération d'une liste d'activités filtrée par critère de recherche
        /// </summary>
        /// <param name="recherche"></param>
        /// <param name="orderByLibelleAsc"></param>
        /// <returns></returns>
        public static List<Activite> GetListeActivite(string recherche = null, bool orderByLibelleAsc = true)
        {
            try
            {
                IQueryable<Activite> query = EntityConnection.GetContext().Activite;

                query.Where(it => (recherche == null || recherche.Trim() == "" || it.Libelle == recherche));

                if (orderByLibelleAsc)
                {
                    query.OrderBy(it => it.Libelle);
                }
                else
                {
                    query.OrderByDescending(it => it.Libelle);
                }
                query.Select(it => it);

                return query.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Recherche d'une activité par son Id
        /// </summary>
        /// <param name="id">Identifiant de l'activité</param>
        /// <returns>L'Activité trouvée</returns>
        public static Activite GetActiviteById(int id)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Activite
                            where it.IdActivite == id
                            select it;

                Activite a = null;

                a = query.First();

                return a;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode d'ajout en base d'une Activité
        /// </summary>
        /// <param name="a">L'activité à ajouter</param>
        /// <returns>L'Activité</returns>
        public static Activite AjouterActivite(Activite a)
        {
            try
            {
                List<CompetenceParActivite> lstCompParActivite = new List<CompetenceParActivite>();
                foreach (CompetenceParActivite c in a.CompetenceParActivite)
                {
                    lstCompParActivite.Add(c);
                }

                if (!EntityStateChecker.IsDetached(a))
                {
                    EntityConnection.GetContext().Activite.Detach(a);
                }
                EntityConnection.GetContext().Activite.AddObject(a);

                foreach (CompetenceParActivite cpa in lstCompParActivite)
                {
                    if (EntityStateChecker.IsDetached(cpa))
                    {
                        EntityConnection.GetContext().CompetenceParActivite.Attach(cpa);
                    }
                    a.CompetenceParActivite.Add(cpa);
                }

                EntityConnection.GetContext().SaveChanges();
                return a;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// Méthode de suppression d'une activité
        /// </summary>
        /// <param name="a">L'activité à supprimer</param>
        public static void SupprimerActivite(Activite a)
        {
            try
            {
                Activite ac = ActiviteOPManager.GetActiviteById(a.IdActivite);
                foreach (CompetenceParActivite cpa in ac.CompetenceParActivite.ToList())
                {
                    CompetenceParActiviteOPManager.SupprimerCompetenceParActivite(cpa);
                }
                EntityConnection.GetContext().Activite.DeleteObject(ac);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
